home *** CD-ROM | disk | FTP | other *** search
- /****************************************************************************/
- /* PRUEBA DEL TECLADO MULTIFUNCION */
- /* */
- /* por: José Antonio Acedo Martín-Grande, miembro de "GOLDY GAMES" */
- /* */
- /* TEL: (91) 6 11 72 71 (MADRID) */
- /****************************************************************************/
-
- /**************************** FICHEROS A INCLUIR ***************************/
-
- #include <dos.h>
- #include <bios.h>
- #include <conio.h>
- #include <stdio.h>
- #include <alloc.h>
- #include <stdlib.h>
-
- /******************************* DEFINICIONES *******************************/
-
- #define ALFANUMERICO 3
- #define GRAFICO 19
- #define ANCHO 37
- #define ALTO 35
-
- /*********************** DECLARACIONES DE FUNCIONES *************************/
-
- void DIBUJA_BLOQUE_CUT(int *, char *, char *);
- void DIBUJA_BLOQUE (int *, char *, char *);
- void VUELCA_PANTALLA(int,int, int,int, int,int, char *);
- void SET_VECTORES(volatile char *);
- void UNSET_VECTORES(void);
- void espera_soltar_tecla(void);
- int espera_pulsar_tecla(void);
- void asigna_modo_video(char);
- void lee_ficheros(const char *, char *);
- void descomprime_dibujo(char *);
- void asigna_rgb(char *);
- void asigna_memoria(void);
- void libera_memoria(void);
- void sin_memoria(void);
- void error_fichero(void);
- void limpia_teclado(void);
- void salir_al_dos(void);
-
- /*************************** VARIABLES GLOBALES /****************************/
-
- char *dir_carga_dibujos;
- char *dir_dibujo1;
- char *dir_dibujo2;
- char *dir_zona_pantalla;
- FILE *handle_dibujos;
- const char dibujo1[] = { "tepract1.pcx" };
- const char dibujo2[] = { "tepract2.pcx" };
- volatile char mapa_teclado[256];
- int pelota [6] = { 0,0, 140,82, 37, 35 };
- int fondo [6] = { 0,0, 0, 0, 320,200 };
-
- /********************************* PROGRAMA *********************************/
-
- void main(void)
- {
- int key_der=0, key_izq=0, key_arr=0, key_aba=0, x, y;
-
- asigna_memoria();
- asigna_modo_video(GRAFICO);
- lee_ficheros(dibujo1, dir_dibujo1);
- lee_ficheros(dibujo2, dir_dibujo2);
- SET_VECTORES(mapa_teclado);
-
- limpia_teclado();
- gotoxy(10,10); printf("DEFINE UNA TECLA PARA:");
-
- gotoxy(17,12); printf(" DERECHA ");
- while(key_der==0 ||
- key_der==key_izq ||
- key_der==key_arr ||
- key_der==key_aba)
- {
- key_der=espera_pulsar_tecla();
- }
-
- gotoxy(17,12); printf(" ");
- espera_soltar_tecla();
-
- gotoxy(17,12); printf("IZQUIERDA");
- while(key_izq==0 ||
- key_izq==key_der ||
- key_izq==key_arr ||
- key_izq==key_aba)
- {
- key_izq=espera_pulsar_tecla();
- }
-
- gotoxy(17,12); printf(" ");
- espera_soltar_tecla();
-
- gotoxy(17,12); printf(" ARRIBA ");
- while(key_arr==0 ||
- key_arr==key_der ||
- key_arr==key_izq ||
- key_arr==key_aba)
- {
- key_arr=espera_pulsar_tecla();
- }
-
- gotoxy(17,12); printf(" ");
- espera_soltar_tecla();
-
- gotoxy(17,12); printf(" ABAJO ");
- while(key_aba==0 ||
- key_aba==key_der ||
- key_aba==key_izq ||
- key_aba==key_arr)
- {
- key_aba=espera_pulsar_tecla();
- }
-
- gotoxy(17,12); printf(" ");
- espera_soltar_tecla();
-
- while(mapa_teclado[1]==0) {
- x=pelota[2]; y=pelota[3];
- if(mapa_teclado[key_der]==1) { x+=8; if(x>= 320) x=0-ANCHO; }
- if(mapa_teclado[key_izq]==1) { x-=8; if(x< 0-ANCHO) x= 319; }
- if(mapa_teclado[key_arr]==1) { y-=8; if(y< 0- ALTO) y= 199; }
- if(mapa_teclado[key_aba]==1) { y+=8; if(y>= 200) y=0- ALTO; }
- pelota[2]=x; pelota[3]=y;
- DIBUJA_BLOQUE (fondo, dir_dibujo2, dir_zona_pantalla);
- DIBUJA_BLOQUE_CUT(pelota, dir_dibujo1, dir_zona_pantalla);
- VUELCA_PANTALLA(0,0, 0,0, 320,200, dir_zona_pantalla);
- }
- salir_al_dos();
- }
-
- void espera_soltar_tecla(void)
- {
- int c, a;
-
- while(1==1)
- {
- a=0;
- for(c=1; c<256; c++)
- {
- a=a|mapa_teclado[c];
- }
- if(a==0) break;
- }
- }
-
- int espera_pulsar_tecla(void)
- {
- int c;
-
- while(1==1)
- {
- for(c=1; c<256; c++)
- {
- if(mapa_teclado[c]==1) return c;
- }
- }
- }
-
- void salir_al_dos(void)
- {
- asigna_modo_video(ALFANUMERICO);
- libera_memoria();
- UNSET_VECTORES();
- exit(0);
- }
-
- void limpia_teclado(void)
- {
- int n;
-
- for(n=0; n<256; n++) { mapa_teclado[n]=0; }
- }
-
- void asigna_memoria(void)
- {
- if((dir_carga_dibujos=(char *)malloc(320*200))==NULL) sin_memoria();
- if((dir_dibujo1 =(char *)malloc(320*200))==NULL) sin_memoria();
- if((dir_dibujo2 =(char *)malloc(320*200))==NULL) sin_memoria();
- if((dir_zona_pantalla=(char *)malloc(320*200))==NULL) sin_memoria();
- }
-
- void libera_memoria(void)
- {
- free(dir_carga_dibujos);
- free(dir_dibujo1);
- free(dir_dibujo2);
- free(dir_zona_pantalla);
- }
-
- void sin_memoria(void)
- {
- asigna_modo_video(ALFANUMERICO);
- libera_memoria();
- printf("No hay suficiente memoria. Libere programas residentes.");
- exit(1);
- }
-
- void error_fichero(void)
- {
- asigna_modo_video(ALFANUMERICO);
- libera_memoria();
- printf("Error leyendo fichero .PCX");
- exit(1);
- }
-
- void asigna_modo_video(char modo) /* asigna el modo de vídeo indicado */
- { /* en la variable "modo" */
- union REGS ent, sal;
-
- ent.h.al = modo;
- ent.h.ah = 0;
- int86(16, &ent, &sal); /* función para asignar el modo de video */
- }
-
- void lee_ficheros(const char *file, char *dir_dibujo)
- {
- char *dir;
-
- dir=dir_carga_dibujos;
- if((handle_dibujos = fopen(file, "rb"))==NULL) error_fichero();
- while(!feof(handle_dibujos)) { *dir++=getc(handle_dibujos); }
- fclose(handle_dibujos);
- descomprime_dibujo(dir_dibujo);
- }
-
- void descomprime_dibujo(char *dir_escritura)
- {
- unsigned char byte;
- char *dir_lectura;
- int columnas, filas, contador;
-
- dir_lectura=dir_carga_dibujos+128; /* inicio del dibujo comprimido */
-
- for(filas=200; filas>0; filas--) {
- columnas=320;
- dir_escritura-=columnas; dir_escritura+=320;
- while(columnas>0) {
- byte=(unsigned)*dir_lectura++;
- if(byte<=192) { *dir_escritura++=byte; columnas--; }
- else {
- contador=byte&63; byte=*dir_lectura++;
- for(;contador>0;contador--) { *dir_escritura++=byte;columnas--; }
- }
- }
- }
- asigna_rgb(dir_lectura+1); /* remapea los 256 colores del dibujo */
- }
-
- void asigna_rgb(char *dir_lectura)
- {
- struct SREGS seg;
- union REGS ent, sal;
- unsigned int n;
- long int dir;
- char *dir_col;
-
- dir_col=dir_lectura; /* divide entre */
- for(n=256*3; n>0; n--) { /* 4 los colores */
- *dir_col=*dir_col >> 2; dir_col++;
- }
-
- dir =(long)dir_lectura; /* obtiene el segmento */
- seg.es=(int)(dir >> 16); /* donde estan los colores */
-
- ent.h.al = 18;
- ent.h.ah = 16;
- ent.x.bx = 0;
- ent.x.cx = 256;
- ent.x.dx = (int)dir_lectura; /* offset de los colores */
- int86x(0x10, &ent, &sal, &seg); /* función para asignar los colores */
- }